package com.hb0730.boot.admin.base.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NamingUtil {
    public static String camelToUnderscore(String camelCase) {
        return camelCase.replaceAll("([a-z])([A-Z])", "$1_$2").toLowerCase();
    }
    public static List<Map<String, Object>> convertToCamelCase(List<Map<String, Object>> dataList) {
        List<Map<String, Object>> result = new ArrayList<>();
        for (Map<String, Object> row : dataList) {
            Map<String, Object> newRow = new HashMap<>();
            for (Map.Entry<String, Object> entry : row.entrySet()) {
                String columnName = entry.getKey();
                String camelCaseColumnName = toCamelCase(columnName);
                newRow.put(camelCaseColumnName, entry.getValue());
            }
            result.add(newRow);
        }
        return result;
    }

    private static String toCamelCase(String columnName) {
        StringBuilder camelCaseString = new StringBuilder();
        boolean nextUpperCase = false;
        for (char c : columnName.toCharArray()) {
            if (c == '_') {
                nextUpperCase = true;
            } else {
                if (nextUpperCase) {
                    camelCaseString.append(Character.toUpperCase(c));
                    nextUpperCase = false;
                } else {
                    camelCaseString.append(Character.toLowerCase(c));
                }
            }
        }
        return camelCaseString.toString();
    }
}